#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// 输入: "Hello, {name}! You have {count} new messages.", {name: "Alice", count: 5}
// 输出: "Hello, Alice! You have 5 new messages."
void template(const char *str, const char *userdata) {
  // 你的实现
  char *name = NULL;
  char *tempname;
  char *tempname1;
  char *savename = NULL;

  char *count_char = NULL;
  char *temp_char = NULL;
  char *save_count = NULL;
  int num = 0;

  //先提取数据
  char *copy_data = strdup(userdata);
  char *copy_line = strdup(str);

  if(*copy_data == '{'){
    copy_data++;
  }

  //1.取结构struct 2.取元素
  int flag = 1;
  char *savedata = NULL;
  char *data = strtok_r(copy_data,",",&savedata);
  while(data){
    if(flag){
      //分割一下
      tempname = strtok_r(data,"\"",&savename);
      //查找'"'
      tempname1 = strchr(savename,'\"');
      *tempname1 = '\0';
      name = savename;
      printf("name = %s\n",name);
    }else{
      temp_char = strtok_r(data,":",&count_char);
      save_count = strchr(count_char,'}');
      *save_count = '\0';
      printf("count = %s",count_char);
    }
    flag = 0;
    data = strtok_r(NULL,",",&savedata);
  }

  //字符串替换
  

  free(copy_data);
  free(copy_line);
}

int main(void)
{
  char* str = "Hello, {name}! You have {count} new messages.";
  char* data = "{name: \"Alice\", count: 5}";
  //先提取数据
  template(str, data);
  
}